﻿using DevExpress.XtraEditors;
using EyFramework.Common;
using EyFramework.Library;
using EyFramework.Sys.Core.Service;
using EyFramework.Sys.Entity;
using System.Windows.Forms;

namespace EyFramework.Main
{
    public partial class FrmChangeSys : FrmDevBase
    {
        public SysList changeSys;

        public FrmChangeSys()
        {
            InitializeComponent();
        }

        private void FrmChangeSys_Load(object sender, System.EventArgs e)
        {
            ShowAllList();
        }

        private void ShowAllList()
        {
            ISysListService sysListService = Globals.GetService<ISysListService>();
            var sysList = sysListService.GetAllActiveSys();
            sysList.ForEach(p =>
            {
                AddSys(p, p.SYS_ID.ToString() == Globals.CurrentUser.SysId);
            });
        }

        private void AddSys(SysList sys, bool isCurrent)
        {
            SimpleButton sbSys = new SimpleButton();
            sbSys.Name = "sys-" + sys.SYS_ID;
            sbSys.Size = new System.Drawing.Size(125, 100);
            sbSys.TabIndex = flpMain.Controls.Count;
            sbSys.Text = sys.SYS_NAME;
            sbSys.Appearance.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            sbSys.Appearance.Options.UseFont = true;
            if (isCurrent)
            {
                sbSys.Text = sbSys.Text + "\r\n(当前正在使用)";
                sbSys.Enabled = false;
            }
            sbSys.Tag = sys;
            sbSys.DoubleClick += SbSys_DoubleClick;
            flpMain.Controls.Add(sbSys);
        }

        private void SbSys_DoubleClick(object sender, System.EventArgs e)
        {
            changeSys = ((SimpleButton)sender).Tag as SysList;
            if (Globals.ConfirmYesNo(string.Format("您正在准备切换到{0}，切换后已经打开的功能将全部关闭，是否继续切换？", changeSys.SYS_NAME)) == DialogResult.Yes)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}